package com.erp.module.caiLiaoZhiLiang.xGCaiLiaoZhiLiang

import android.content.Context
import android.util.Log
import com.erp.bean.CaiLiaoZhiLiangBean
import com.erp.mclass.UnCeHandler
import com.erp.Interface.OnUserDataInterface
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.module.caiLiaoZhiLiang.CaiLiaoZhiLiangContract
import com.erp.module.caiLiaoZhiLiang.CaiLiaoZhiLiangModel
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2018/5/15/015.
 * 作者：刘洋
 */
class IXiugaiCailiaoZhiliangKP(val context: Context, val KView: CaiLiaoZhiLiangContract.XiuGaiCaiLiaoZhiLiangKView, val mbean: CaiLiaoZhiLiangBean, val isXingJian: Boolean) : CaiLiaoZhiLiangContract.XiuGaiCailiaoZhiliangKP {

    val modle : CaiLiaoZhiLiangContract.CaiLiaoZhiLiangMP= CaiLiaoZhiLiangModel()
    companion object {
        var huopings = ""
        var gongyingshangs = ""
        var cailiaos = ""
        var wentis = ""
    }

    //获取货品编码数据
    override fun getHuoping() {
        UnCeHandler.buzhou="getHuoping"
        var Ids: ArrayList<String> = ArrayList<String>()
        var Names: ArrayList<String> = ArrayList<String>()
        if (huopings == "") {
            modle.getHuopingBianma(context, object : OnUserDataInterface {
                override fun onUserData(data: String?) {
                    Log.d("TAAAG","data="+data )
                    if (data == null) {
                        return
                    }
                    huopings = data
                    ziliaojson(huopings, Ids, Names)
                    KView.HuopingOk(Ids, Names)
                }

                override fun onNot(throwable: String?) {
                    Log.d("TAAAG","th="+throwable.toString())
                    KView.HuopingNo()
                }

            })
        } else {
            ziliaojson(huopings, Ids, Names)
            KView.HuopingOk(Ids, Names)
        }
    }

    //获取供应商数据
    override fun getGongyingshang() {
        UnCeHandler.buzhou="getGongyingshang"
        var Ids: ArrayList<String> = ArrayList<String>()
        var Names: ArrayList<String> = ArrayList<String>()
        if (gongyingshangs == "") {
            modle.getGongyingshangLieBiao(context, object : OnUserDataInterface {
                override fun onUserData(data: String?) {
                    if (data == null) {
                        return
                    }
                    gongyingshangs = data
                    ziliaojson(gongyingshangs, Ids, Names)
                    KView.GongyingshangOk(Ids, Names)
                }

                override fun onNot(throwable: String?) {
                    Log.d("TAAAG","th="+throwable.toString())
                    KView.GongyingshangNo()
                }

            })
        } else {
            ziliaojson(gongyingshangs, Ids, Names)
            KView.GongyingshangOk(Ids, Names)
        }
    }

    //获取材料类别数据
    override fun getCailiaoLeibie() {
        UnCeHandler.buzhou="getCailiaoLeibie"
        var Ids: ArrayList<String> = ArrayList<String>()
        var Names: ArrayList<String> = ArrayList<String>()
        if (cailiaos == "") {
            modle.getCailiaoLeibie(context, object : OnUserDataInterface {
                override fun onUserData(data: String?) {
                    if (data == null) {
                        return
                    }
                    cailiaos = data
                    ziliaojson(cailiaos, Ids, Names)
                    KView.CailiaoLeibieOk(Ids, Names)
                }

                override fun onNot(throwable: String?) {
                    KView.CailiaoLeibieNo()
                }

            })
        } else {
            ziliaojson(cailiaos, Ids, Names)
            KView.CailiaoLeibieOk(Ids, Names)
        }
    }

    //获取问题代码数据
    override fun getWentiDaima() {
        UnCeHandler.buzhou="getWentiDaima"
        var Ids: ArrayList<String> = ArrayList<String>()
        var Names: ArrayList<String> = ArrayList<String>()
        if (wentis == "") {
            modle.getWentiDaima(context, object : OnUserDataInterface {
                override fun onUserData(data: String?) {
                    if (data == null) {
                        return
                    }
                    wentis = data
                    ziliaojson(wentis, Ids, Names)
                    KView.WentiDaimaOk(Ids, Names)
                }

                override fun onNot(throwable: String?) {
                    KView.WentiDaimaNo()
                }

            })
        } else {
            ziliaojson(wentis, Ids, Names)
            KView.WentiDaimaOk(Ids, Names)
        }
    }

    override fun XingjianXiugaiCailiao(bean: CaiLiaoZhiLiangBean,deletephoto : List<CaiLiaoZhiLiangBean.photo>) {
        UnCeHandler.buzhou="XingjianXiugaiCailiao"
        bean.listphoto=mbean.listphoto
        if (isXingJian) {
            XingjianCailiao(bean, deletephoto)
        } else {
            bean.id = mbean.id
            XiugaiCailiao(bean, deletephoto)
        }
    }
    //新建材料质量
    fun XingjianCailiao(bean: CaiLiaoZhiLiangBean,deletephoto : List<CaiLiaoZhiLiangBean.photo>) {
        modle.setGongyingCailiaoZhiliang(context, 1, bean, deletephoto,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                Log.d("XingjianCailiao","onUserData="+data.toString())
                KView.XingjianCailiaoOk()
            }

            override fun onNot(throwable: String?) {
                Log.d("XingjianCailiao","ex="+throwable.toString())
                KView.XingjianCailiaoNo()
            }

        })
    }

    //修改材料质量
    fun XiugaiCailiao(bean: CaiLiaoZhiLiangBean,deletephoto : List<CaiLiaoZhiLiangBean.photo>) {
        modle.setGongyingCailiaoZhiliang(context, 2, bean, deletephoto,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                if(data==null){
                    return
                }
                Log.d("TAAAG","ex="+Myutil.decodeUnicode(data))
                datajson(data)
                KView.XiugaiCailiaoOk()
            }

            override fun onNot(throwable: String?) {
                Log.d("TAAAG","ex="+Myutil.decodeUnicode(throwable.toString()))
                KView.XiugaiCailiaoNo()
            }

        })
    }

    //解析修改后的json
    fun datajson(data: String) {
        UnCeHandler.buzhou = "getJson"
        try {
            val jo = JSONObject(data)
            mbean.id = JsonUtil.getvolue(jo, "id")
            mbean.iu_id = JsonUtil.getvolue(jo, "iu_id")
            mbean.gi_id = JsonUtil.getvolue(jo, "gi_id")
            mbean.mc_2_id = JsonUtil.getvolue(jo, "mc2_id")
            mbean.qi_id = JsonUtil.getvolue(jo, "qi_id")
            mbean.jianyan_time = JsonUtil.getvolue(jo, "test_time")
            mbean.cailiao_shuliang = JsonUtil.getvolue(jo, "cailiao_shuliang")
            mbean.choucha_shuliang = JsonUtil.getvolue(jo, "choucha_shuliang")
            mbean.hege_shuliang = JsonUtil.getvolue(jo, "hege_shuliang")
            mbean.quality_decision = JsonUtil.getvolue(jo, "quality_decision")
            mbean.process_mode = JsonUtil.getvolue(jo, "process_mode")
            mbean.solution = JsonUtil.getvolue(jo, "solution")
            mbean.iu_name = JsonUtil.getvolue(jo, "iu_name")
            mbean.gi_name = JsonUtil.getvolue(jo, "gi_name")
            mbean.mc2_name = JsonUtil.getvolue(jo, "mc2_name")
            mbean.qi_name = JsonUtil.getvolue(jo, "qi_name")
            mbean.description = JsonUtil.getvolue(jo, "description")
            var jsp = JsonUtil.getjsvolue(jo, "files")
            val photos= ArrayList<CaiLiaoZhiLiangBean.photo>()
            if (jsp != null) {
                for (j in 0 until jsp.length()) {
                    val jop = jsp.get(j) as JSONObject
                    val photo = CaiLiaoZhiLiangBean.photo()
                    photo.fid = JsonUtil.getvolue(jop, "fid")
                    photo.filename = JsonUtil.getvolue(jop, "filename")
                    photo.uri = JsonUtil.getvolue(jop, "uri")
                    photos.add(photo)
                }
            }
            mbean.listphoto=photos
        } catch (e: JSONException) {
            Log.d("TAAAG", "e=$e---------------")
            e.printStackTrace()
        }
    }

    //解析资料数据json
    fun ziliaojson(data: String, ids: ArrayList<String>, names: ArrayList<String>) = try {
        val jo = JSONObject(data)
        val keys = jo.keys()
        while (keys.hasNext()) {
            val key = keys.next() as String
            ids.add(key)
            names.add(JsonUtil.getvolue(jo, key))
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}